<template>
	<view>
		<u-navbar :is-back="false" title-color="#333" title-bold title-size="36" title="在线咨询"
			:border-bottom="false"></u-navbar>

		<!-- <view class="xx-div">
			<view class="xx-item2 flex" v-for="(item,index) in datalist2" :key="index" @click="showmo(item)">
				<view class="headimg">
					<u-badge v-if="item.is_read!=1" is-dot type="error" :offset="[0,0]"></u-badge>
					<image class="headimg" :src="item.image" mode="aspectFill"></image>
				</view>
				<view class="u-m-l-20">
					<view class="flex flex_sb">
						<view class="font_weight">
							{{item.name}}
						</view>
						<view class="font_12_999">
							{{item.time_trans}}
						</view>
					</view>
					<view class="font_12_999 line_1 u-m-t-12 xx-cen2">
						{{item.content}}
					</view>
				</view>
			</view>
		</view> -->
		<!-- <u-modal title="消息" v-model="xxshow" :content="content"></u-modal> -->
		
		<view class="xx-div">
			<view class="xx-item2 flex" v-for="(item,index) in conversationData" :key="index" @click="toMessage(item)">
				<view class="headimg">
					<u-badge v-if="item.unreadCount!=0" is-dot type="error" :offset="[0,0]"></u-badge>
					<!-- <view class="b7" v-if="item.unreadCount!=0">{{item.unreadCount}}</view> -->
					<image :src="item.userProfile.avatar" class="headimg"></image>
				</view>
				<view class="u-m-l-20">
					<view class=" font_weight">{{item.userProfile.nick}}</view>
					<view class="font_12_999  line_1 u-m-t-12 xx-cen2">{{item.lastMessage.messageForShow}}</view>
				</view>
			</view>
		</view>
		
		<u-tabbar active-color="#FA3B21" :list="tabbar" :mid-button="true"></u-tabbar>
	</view>
</template>

<script>
	import TIM from '../../static/IM-SDK/tim-wx.js'
	export default {
		data() {
			return {
				tabbar: '',
				content: '',
				xxshow: false,
				datalist2: [],

				userID: '',
				autoHeight: 0,
				userSign: '',
				conversationData: []
			}
		},

		onLoad() {
			this.tabbar = [{
					iconPath: "/static/img/tabBar/1-2.png",
					selectedIconPath: "/static/img/tabBar/1-1.png",
					text: '首页',
					pagePath: "/pages/index/index"
				}, {
					iconPath: "/static/img/tabBar/2-2.png",
					selectedIconPath: "/static/img/tabBar/2-1.png",
					text: '找律师',
					pagePath: "/pages/lawyer/lawyer"
				}, {
					iconPath: "/static/img/tabBar/3-1.png",
					selectedIconPath: "/static/img/tabBar/3-1.png",
					text: 'AI模型',
					midButton: true,
					pagePath: "/pages/AI/AI"
				}, {
					iconPath: "/static/img/tabBar/4-1.png",
					selectedIconPath: "/static/img/tabBar/4-2.png",
					text: '在线咨询',
					// count: 2,
					isDot: false,
					pagePath: "/TXIM/pages/consultation/consultation"
				}, {
					iconPath: "/static/img/tabBar/5-2.png",
					selectedIconPath: "/static/img/tabBar/5-1.png",
					text: '我的',
					pagePath: "/pages/mine/mine"
				},

			]


			this.autoHeight = uni.getSystemInfoSync().statusBarHeight;
			this.getInfo()
			
			if (TIM.EVENT.SDK_READY == 'sdkStateReady') {
				this.getConversationList();
			}
		},
		onShow() {
			this.getList()
		},
		methods: {

			//个人信息
			getInfo() {
				this.$api({
					url: '/getUserInfo',
					method: 'get',
					data: {
						token: uni.getStorageSync("token")
					},
				}).then(res => {
					if (res.code == 1) {
						this.$api({
							url: '/user/getImUserSign',
							method: 'post',
							data: {
								token: uni.getStorageSync("token"),
								user_id: res.data.id
							},
						}).then(res => {
							if (res.code == 1) {
								this.userSign = res.data.userSign
								this.userID = res.data.userId
								this.timLogin();
							}
						})
					}
				})
			},

			/* 登录 */
			timLogin() {
				var that = this;
				that.tim.login({
					userID: that.userID, //登录时的用户ID
					userSig: that.userSign
				}).then(function(imResponse) {
					console.log("登录结果", imResponse)
					//获取会话列表(必须等待TIM处于Ready状态才可获取，所以加了这么一行监听);
					that.tim.on(TIM.EVENT.SDK_READY, function(event) {
						that.getConversationList();
					}, that);


					uni.hideLoading();
				})
			},
			/* 获取会话列表 */
			getConversationList() {
				var that = this;
				let promise = that.tim.getConversationList();
				promise.then(function(imResponse) {
					const conversationList = imResponse.data.conversationList; // 全量的会话列表，用该列表覆盖原有的会话列表
					that.conversationData = conversationList;
					console.log("回话列表：", conversationList);
				});
			},
			/* 跳转消息页 */
			toMessage(item) {
				//垃圾组件不触发 只能嵌套触发的垃圾东西
				uni.$on('send', () => {
					uni.$emit("conversationData", item);
				})
				uni.navigateTo({
					url: '/TXIM/pages/chat/chat'
				})
			},

			showmo(item) {
				this.xxshow = true
				this.content = item.content
			},
			getList() {
				this.$api({
					url: '/lawyer/msg_list',
					method: 'get',
					data: {
						token: uni.getStorageSync("token"),
						type: 1, //1用户登入 2律师登入
					},
				}).then(res => {
					if (res.code == 1) {
						this.datalist2 = res.data.list
					}
				})
			},
			chat() {
				uni.navigateTo({
					url: "/pages/consultation/chat"
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.a8 {
		border-bottom: 1upx solid #F1F1F1;
		background-color: #fff;
		height: 150upx;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.a9 {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 20%;
	}

	.b2 {
		height: 80upx;
		width: 80upx;
		// margin-top: 10upx;
	}

	.b3 {
		height: 80upx;
		width: 80upx;
		border-radius: 50%;
	}

	.b4 {
		width: 70%;
		display: flex;
		flex-direction: column;
	}

	.b5 {
		font-size: 28upx;
		color: #101010
	}

	.b6 {
		font-size: 22upx;
		color: #999999;
		margin-top: 6upx;
		letter-spacing: 1upx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.b7 {
		position: absolute;
		background-color: red;
		height: 30upx;
		width: 30upx;
		border-radius: 100%;
		color: #FFFFFF;
		text-align: center;
		line-height: 30upx;
		font-size: 22upx;
		left: 100upx;
	}





	.xx-div {
		.xx-item {
			width: 686rpx;
			padding: 24rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin: 0 auto;
			margin-top: 20rpx;

			.xx-title {
				font-weight: bold;
				font-size: 32rpx;
				color: #333333;
				width: 512rpx;
			}

			.xx-cen {
				line-height: 40rpx;
			}

			.xx-btn {
				font-size: 28rpx;
				color: #5389ED;
			}
		}

		.xx-item2 {
			width: 686rpx;
			padding: 30rpx 24rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin: 0 auto;
			margin-top: 20rpx;

			.headimg {
				width: 88rpx;
				height: 88rpx;
				border-radius: 50%;
				position: relative;
				background-color: #eee;
			}

			.xx-cen2 {
				width: 528rpx;
			}
		}
	}
</style>